﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using Web.Class;

namespace Web.Controls
{
    public partial class UCLHJinList2QS : System.Web.UI.UserControl,IUCLHOperate
    {
        #region 属性

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX { get; set; }

        /// <summary>
        /// 来函流程步骤
        /// </summary>
        public eLaiHanLCBH LaiHanLC
        {
            get
            {
                return ViewState["LaiHanLC"] == null ? eLaiHanLCBH.未登记 : (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), Convert.ToString(ViewState["LaiHanLC"]));
            }
            set
            {
                ViewState["LaiHanLC"] = value.ToString();
            }
        }

        /// <summary>
        /// 设置复选框列显隐
        /// </summary>
        public bool ShowCheck;

        /// <summary>
        /// 记录来函编号
        /// </summary>
        public string LaiHanBH
        {
            get
            {
                return Convert.ToString(ViewState["LaiHanBH"]);
            }
            set
            {
                ViewState["LaiHanBH"] = value;
            }
        }

        /// <summary>
        /// 记录井原用户号
        /// </summary>
        public string YuanYongHuH
        {
            get
            {
                return Convert.ToString(ViewState["YuanYongHuH"]);
            }
            set
            {
                ViewState["YuanYongHuH"] = value;
            }
        }
        /// <summary>
        /// 记录目标用户号
        /// </summary>
        public string TargetYongHuH
        {
            get
            {
                return Convert.ToString(ViewState["TargetYongHuH"]);
            }
            set
            {
                ViewState["TargetYongHuH"] = value;
            }
        }

        /// <summary>
        /// 设置列表高度
        /// </summary>
        public int ListHeight
        {
            set
            {
                this.gvList.Settings.VerticalScrollableHeight = value;
            }
        }

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }

        /// <summary>
        /// 移除
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            //清除结果集中对应记录
            for (int i = 0; i < this.dtSource.Rows.Count; i++)
            {
                if (this.dtSource.Rows[i]["S_JINGBH"].ToString() == e.CommandArgument.ToString())
                {
                    this.dtSource.Rows.RemoveAt(i);
                }
            }
            this.BindData();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 配置控件
        /// </summary>
        public void InitControl()
        {
            if (BusinessTool.WType == 1)
            {
                this.gvList.Columns[1].Caption = "取水口号";
                this.gvList.Columns[2].Visible = false;
                this.gvList.Columns[3].Caption = "取水口地址";
                this.gvList.Columns[3].Width = new Unit("70%");
            }
            else if (BusinessTool.WType == 2)
            {
                this.gvList.Columns[1].Caption = "井号";
                this.gvList.Columns[2].Visible = true;
                this.gvList.Columns[3].Caption = "井地址";
                if (this.LaiHanLX == eLaiHanLX.水资源深井类型修改)
                {
                    this.gvList.Columns[4].Visible = true;
                    this.gvList.Columns[5].Visible = true;
                    this.gvList.Columns[6].Visible = false;
                    this.gvList.Columns[7].Visible = false;
                    this.gvList.Columns[1].Width = Unit.Parse("20%");
                    this.gvList.Columns[2].Width = Unit.Parse("20%");
                    this.gvList.Columns[3].Width = Unit.Parse("20%");
                    this.gvList.Columns[4].Width = Unit.Parse("20%");
                    this.gvList.Columns[5].Width = Unit.Parse("20%");
                }
                else if (this.LaiHanLX == eLaiHanLX.水资源井性质修改)
                {
                    this.gvList.Columns[4].Visible = false;
                    this.gvList.Columns[5].Visible = false;
                    this.gvList.Columns[6].Visible = true;
                    this.gvList.Columns[7].Visible = true;
                    this.gvList.Columns[1].Width = Unit.Parse("20%");
                    this.gvList.Columns[2].Width = Unit.Parse("20%");
                    this.gvList.Columns[3].Width = Unit.Parse("20%");
                    this.gvList.Columns[6].Width = Unit.Parse("20%");
                    this.gvList.Columns[7].Width = Unit.Parse("20%");
                }
                else
                {
                    //this.gvList.Columns[4].Visible = false;
                    //this.gvList.Columns[5].Visible = false;
                    //this.gvList.Columns[6].Visible = false;
                    //this.gvList.Columns[7].Visible = false;
                    this.gvList.Columns[1].Width = Unit.Parse("20%");
                    this.gvList.Columns[2].Width = Unit.Parse("30%");
                    this.gvList.Columns[3].Width = Unit.Parse("50%");
                }
            }

            
        }

        /// <summary>
        /// 返回井类型,井性质文本
        /// </summary>
        /// <param name="type">0:井类型 1:井性质</param>
        /// <param name="value">编号</param>
        /// <returns>返回文本</returns>
        protected string GetJLXJXZText(int type, string value)
        {
            string text = "";
            if (type == 0)//井类型
            {
                text = Enum.Parse(typeof(eJinLX), value).ToString();
            }
            else if (type == 1)//井性质
            {
                text = Enum.Parse(typeof(eJinXZ), value).ToString();
            }
            return text;
        }

        /// <summary>
        ///重新绑定
        /// </summary>
        public void BindData()
        {
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            //控制复选框列
            for (int i = 0; i < this.gvList.VisibleRowCount; i++)
            {
                ASPxCheckBox cb = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "cb");
                if (null != cb) cb.Visible = ShowCheck;
            }
        }

        /// <summary>
        /// 清空数据
        /// </summary>
        public void ClearInfo()
        {
            this.dtSource = null;
            this.gvList.DataSource = null;
            this.gvList.DataBind();
        }

        public ValidateRS ValidateAdd(string jinbh)
        {
            ValidateRS rs = new ValidateRS();

            if (this.LaiHanLX == eLaiHanLX.水资源井性质修改 || this.LaiHanLX == eLaiHanLX.水资源深井类型修改 || this.LaiHanLX == eLaiHanLX.水资源注销井恢复)
            {
                List<JinXX> list = this.SelectedJinList();
                foreach (var item in list)
                {
                    if (item.JH == jinbh)
                    {
                        rs.IsSuccess = false;
                        rs.Msg = "该井已在列表中";
                        return rs;
                    }
                }
            }
            rs.IsSuccess = true;
            return rs;
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <returns></returns>
        public ValidateRS Validate()
        {
            ValidateRS rs = new ValidateRS();
            /*
            if (this.LaiHanLX == eLaiHanLX.水资源销井)
            {
                if (this.dtSource.Rows.Count == 0)
                {
                    rs.Msg = "当前用户没有井,不需要销井。";
                    return rs;
                }
                else
                {
                    if (this.SelectedJinList().Count == 0)
                    {
                        rs.Msg = "请选择要销的井。";
                        return rs;
                    }
                }
            }
            else */
            if (this.LaiHanLX == eLaiHanLX.水资源井性质修改 || this.LaiHanLX == eLaiHanLX.水资源深井类型修改 || this.LaiHanLX == eLaiHanLX.水资源注销井恢复)
            {
                //if (this.dtSource == null || this.dtSource.Rows.Count == 0)
                if (this.SelectedJinList().Count == 0)
                {
                    rs.Msg = "请选择要操作的井";
                    return rs;
                }
            }
            rs.IsSuccess = true;
            return rs;
        }

        /// <summary>
        /// 获取所有选中的井
        /// </summary>
        /// <returns></returns>
        private List<JinXX> SelectedJinList()
        {
            List<JinXX> list = new List<JinXX>();
            for (int i = 0; i < this.gvList.VisibleRowCount; i++)
            {
                ASPxCheckBox cb = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "cb");
                HiddenField hfJH = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfJH");
                HiddenField hfYHH = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfYHH");
                HiddenField hfLBH = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfLBH");
                HiddenField hfJDZ = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfJDZ");
                HiddenField hfJXZ_old = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfJXZ_old");
                HiddenField hfJXZ_new = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfJXZ_new");
                HiddenField hfJLX_old = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfJLX_old");
                HiddenField hfJLX_new = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfJLX_new");
                if (cb.Checked || !cb.Visible)
                {
                    JinXX it = new JinXX();
                    it.JH = hfJH.Value;
                    it.YHH = hfYHH.Value;
                    it.LBH = hfLBH.Value;
                    it.JDZ = hfJDZ.Value;
                    it.JXZ_OLD = hfJXZ_old.Value;
                    it.JXZ_NEW = hfJXZ_new.Value;
                    it.JLX_OLD = hfJLX_old.Value;
                    it.JLX_NEW = hfJLX_new.Value;
                    list.Add(it);
                }
            }
            return list;
        }

        public void Add(string jinhao, string yonghuh, string jindz, string laobianh, string jinxz_old, string jinxz_new, string jinlx_old, string jinlx_new)
        {
            if (this.dtSource == null)
            {
                this.dtSource = new DataTable();
                this.dtSource.Columns.Add("S_JINGBH");
                this.dtSource.Columns.Add("S_YONGHUH");
                this.dtSource.Columns.Add("S_LAOBIANH");
                this.dtSource.Columns.Add("S_JINGDIZ");
                this.dtSource.Columns.Add("S_JINXZ_OLD");
                this.dtSource.Columns.Add("S_JINXZ_NEW");
                this.dtSource.Columns.Add("S_JINLX_OLD");
                this.dtSource.Columns.Add("S_JINLX_NEW");
            }
            DataRow row = this.dtSource.NewRow();
            row["S_JINGBH"] = jinhao;
            row["S_YONGHUH"] = yonghuh;
            row["S_LAOBIANH"] = laobianh;
            row["S_JINGDIZ"] = jindz;
            row["S_JINXZ_OLD"] = jinxz_old;
            row["S_JINXZ_NEW"] = jinxz_new;
            row["S_JINLX_OLD"] = jinlx_old;
            row["S_JINLX_NEW"] = jinlx_new;
            this.dtSource.Rows.Add(row);
            this.BindData();
            //this.SetChecked(true);
        }

        /// <summary>
        /// 根据来函加载信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            if (laihanxx.ID == 0)
            {
                this.LaiHanLC = eLaiHanLCBH.未登记;
            }
            else
            {
                this.LaiHanLC = (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), laihanxx.I_LIUCHENGBH.ToString());
                this.LaiHanBH = laihanxx.S_LAIHANBH;
                if (this.LaiHanLX == eLaiHanLX.水资源注销井恢复 || this.LaiHanLX == eLaiHanLX.水资源井性质修改 || this.LaiHanLX == eLaiHanLX.水资源深井类型修改)
                {
                    this.dtSource = new SpHelper().P_QS_JINYHXXHQ(laihanxx.S_LAIHANBH, null, (int)laihanxx.I_LAIHANLX).Tables[0];
                    if (laihanxx.S_LAIHANBH != null)
                    {
                        this.gvList.Columns[8].Visible = false;
                    }
                    this.BindData();
                }
                else if (this.LaiHanLX == eLaiHanLX.水资源销户)
                {
                    this.dtSource = new SpHelper().P_QS_JINBIAOHQ(2, laihanxx.S_YONGHUBH, null).Tables[0];
                    this.BindData();
                }
            }

            if (Convert.ToInt32(this.LaiHanLC) >= 1)
            {
                this.gvList.Columns[0].Visible = false;
            }

        }
        /*
        /// <summary>
        /// 获取申请信息
        /// </summary>
        /// <returns></returns>
        public List<object> GetShengQingXX()
        {
            List<object> list = new List<object>();
            List<string> bhs = this.SelectedJinList();
            for (int i = 0; i < bhs.Count; i++)
            {
                if (this.LaiHanLX == eLaiHanLX.水资源销井)
                {
                    QS_LH_XIAOJINSJXX obj = new QS_LH_XIAOJINSJXX();
                    obj.S_LAIHANBH = this.LaiHanBH;
                    obj.S_JINGBH = bhs[i];
                    list.Add(obj);
                }
                else if (this.LaiHanLX == eLaiHanLX.水资源注销井恢复)
                {
                    QS_LH_XIAOJINHFSJXX obj = new QS_LH_XIAOJINHFSJXX();
                    obj.S_LAIHANBH = this.LaiHanBH;
                    obj.S_JINGBH = bhs[i];
                    list.Add(obj);
                }
                else if (this.LaiHanLX == eLaiHanLX.水资源井性质修改)
                {
                    QS_LH_JINZTXX obj = new QS_LH_JINZTXX();
                    obj.S_LAIHANBH = this.LaiHanBH;
                    obj.S_JINBH = bhs[i];
                    obj.
                    obj.S_YUANYHH = this.YuanYongHuH;
                    obj.S_MUBIAOYHH = this.TargetYongHuH;
                    list.Add(obj);
                }
            }
            return list;
        }
        */
        /// <summary>
        /// 添加申请信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void CreateShengQingXX(YS_LH_LAIHANXX laihanxx)
        {
            if (this.LaiHanLX == eLaiHanLX.水资源井性质修改)
            {
                //清除附表旧信息
                QS_LH_JINZTXXProcess bllT = new QS_LH_JINZTXXProcess();
                bllT.DeleteQS_LH_JINZTXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
                //添加附表新信息
                List<JinXX> list = this.SelectedJinList();
                foreach (JinXX item in list)
                {
                    QS_LH_JINZTXX t = new QS_LH_JINZTXX();
                    t.S_LAIHANBH = laihanxx.S_LAIHANBH;
                    t.S_JINBH = item.JH;
                    t.S_JINZTYUAN = item.JXZ_OLD;
                    t.S_JINZTXIN = item.JXZ_NEW;
                    t.D_BIANGENGRQ = DateTime.Now;
                    bllT.InsertOrUpdate(t);
                }
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源深井类型修改)
            {
                //清除附表旧信息
                QS_LH_JINLXXXProcess bllT = new QS_LH_JINLXXXProcess();
                bllT.DeleteQS_LH_JINLXXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
                //添加附表新信息
                List<JinXX> list = this.SelectedJinList();
                foreach (JinXX item in list)
                {
                    QS_LH_JINLXXX t = new QS_LH_JINLXXX();
                    t.S_LAIHANBH = laihanxx.S_LAIHANBH;
                    t.S_JINBH = item.JH;
                    t.S_JINLXYUAN = item.JLX_OLD;
                    t.S_JINLXXIN = item.JLX_NEW;
                    t.D_BIANGENGRQ = DateTime.Now;
                    bllT.InsertOrUpdate(t);
                }
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源注销井恢复)
            {
                //先清除该来函旧的销表水表记录
                QS_LH_XIAOJINHFSJXXProcess bllT2 = new QS_LH_XIAOJINHFSJXXProcess();
                bllT2.DeleteQS_LH_XIAOJINHFSJXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
                //添加附表新信息
                List<JinXX> list = this.SelectedJinList();
                foreach (JinXX item in list)
                {
                    QS_LH_XIAOJINHFSJXX t = new QS_LH_XIAOJINHFSJXX();
                    t.S_LAIHANBH = laihanxx.S_LAIHANBH;
                    t.S_JINGBH = item.JH;
                    bllT2.InsertOrUpdate(t);
                }
            }
            //if (this.LaiHanLX == eLaiHanLX.水资源销井)
            //{
            //    //先清除该来函旧的销表水表记录
            //    QS_LH_XIAOJINSJXXProcess bllT2 = new QS_LH_XIAOJINSJXXProcess();
            //    bllT2.DeleteQS_LH_XIAOJINSJXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
            //    //添加销表水表记录
            //    //设置来函编号
            //    this.LaiHanBH = laihanxx.S_LAIHANBH;
            //    List<object> list = this.GetShengQingXX();
            //    foreach (QS_LH_XIAOJINSJXX item in list)
            //    {
            //        bllT2.InsertOrUpdate(item);
            //    }
            //}
            /*
            */
            //else if (this.LaiHanLX == eLaiHanLX.水资源井过户)
            //{
            //    //先清除该来函旧的销表水表记录
            //    QS_LH_JINGUOHXXProcess bllT2 = new QS_LH_JINGUOHXXProcess();
            //    bllT2.DeleteQS_LH_JINGUOHXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
            //    //添加销表水表记录
            //    //设置来函编号
            //    this.LaiHanBH = laihanxx.S_LAIHANBH;
            //    List<object> list = this.GetShengQingXX();
            //    foreach (QS_LH_JINGUOHXX item in list)
            //    {
            //        bllT2.InsertOrUpdate(item);
            //    }
            //}
        }

        /// <summary>
        /// 用于从他处传值到本控件属性,此处用于登记前获取原用户与目标用户
        /// </summary>
        /// <param name="values"></param>
        public void RegistMemberValue(params object[] values)
        {
            this.YuanYongHuH = values[0].ToString();
            this.TargetYongHuH = values[1].ToString();
        }

        /// <summary>
        /// 获取来函主表信息
        /// </summary>
        /// <param name="laihanid">ID</param>
        /// <param name="liuchengzt">0:收件 1:收件并登记</param>
        /// <param name="msg">输出消息,正常为空</param>
        /// <returns>null:验证失败 !null:成功返回</returns>
        public YS_LH_LAIHANXX GetLAIHANXX(int laihanid, int liuchengzt, out string msg)
        {
            YS_LH_LAIHANXX laihan = new YS_LH_LAIHANXX();
            string laihanbh = "";
            string xukebh = "";
            msg = "";
            laihan.ID = laihanid;
            if (laihanid == 0)
            {
                string zd, glk, ywy;
                PageTools.GetLHDJZDGLKYWY(null, out zd, out glk, out ywy);
                laihan.S_ZHANDIANBH = zd;
                laihan.S_GUANLIKBH = glk;
                laihan.I_YWY = int.Parse(ywy);
                laihan.S_YWY = BusinessTool.GetYWYName(ywy);
                //laihan.S_YHYWY = ywy;
                //DataSet numset = new SpHelper().P_YS_LH_XUKEBH(glk, 1, 0, (BusinessTool.GetWaterYM() / 100).ToString(), out xukebh, out laihanbh);
                new SpHelper().P_ALL_LH_BHSC(BusinessTool.WType, (int)this.LaiHanLX, BusinessTool.GetWaterYM() / 100, out laihanbh);
                laihan.I_XT = BusinessTool.WType;
                laihan.D_SHURURQ = DateTime.Now;
                laihan.D_LAIHANRQ = DateTime.Now;
                laihan.I_JLZT = 0;
                laihan.I_LAIHANLX = (int)this.LaiHanLX;
                laihan.I_SHURUR = LoginInfo.CurrentUser.UserID;
                laihan.I_AUTOTYPE = 0;
                laihan.S_LAIHANBH = laihanbh;
                laihan.S_BEIYONG1 = xukebh;
                laihan.S_XUKEBH = xukebh;
                laihan.I_LIUCHENGZT = (int)eLaiHanLCZT.流程中;
                laihan.I_TZY = laihan.I_Y = BusinessTool.GetWaterYM() / 100;
                laihan.I_TZM = laihan.I_M = BusinessTool.GetWaterYM() % 100;
                //laihan.S_BEIZHU = this.txtJinBH.Text;
                laihan.S_YONGHUBH = null;
            }
            else
            {
                laihan = new YS_LH_LAIHANXXProcess().SelectYS_LH_LAIHANXX(laihan.ID);
            }
            laihan.I_LIUCHENGBH = liuchengzt;
            laihan.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
            laihan.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            return laihan;
        }


        #endregion

        private class JinXX
        {
            public string JH { get; set; }
            public string YHH { get; set; }
            public string LBH { get; set; }
            public string JDZ { get; set; }

            public string JXZ_OLD { get; set; }
            public string JXZ_NEW { get; set; }
            public string JLX_OLD { get; set; }
            public string JLX_NEW { get; set; }
            public DateTime BGRQ { get; set; }
        }
    }
}